﻿using Hotel.BLL;
using Hotel.DAL;
using Hotel.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Hotel.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MemberController : ControllerBase
    {
        public MemberController(MemberBLL memberBLL)
        {
            MemberBLL = memberBLL;
        }

        public MemberBLL MemberBLL { get; }


        /// <summary>
        /// 会员添加
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public int MemberAdd(Member members)
        {
            try
            {
                return MemberBLL.MemberAdd(members);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 会员列表显示
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="levelId"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public IActionResult GetMembers(int? id = null, string? name = "", int? levelId = null)
        {
            try
            {
                var res = MemberBLL.GetMembers(id, name, levelId);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 会员等级添加
        /// </summary>
        /// <param name="memberLevel"></param>
        /// <returns></returns>
        [HttpPost]
        public int MemberLevelAdd(MemberLevel memberLevel)
        {
            try
            {
                return MemberBLL.MemberLevelAdd(memberLevel);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 会员等级列表
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet]
        public IActionResult GetMemberLevels()
        {
            try
            {
                var res = MemberBLL.GetMemberLevels();
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据id删除会员信息
        /// </summary>
        /// <param name="id">会员id</param>
        /// <returns>返回影响的行数</returns>
        [HttpDelete]
        public IActionResult DelMemberItem(int id)
        {
            try
            {
                int res = MemberBLL.DelMemberItem(id);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 根据会员id获取会员实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMemberInfo(int id)
        {
            try
            {
                var res = MemberBLL.GetMemberInfo(id);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改会员
        /// </summary>
        /// <param name="member"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateMemberInfo(Member member)
        {
            try
            {
                int res = MemberBLL.UpdateMemberInfo(member);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 会员权益增加
        /// </summary>
        /// <param name="memberRight">权益实体</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult MemberRightAdd(MemberRight memberRight)
        {
            try
            {
                int res = MemberBLL.MemberRightAdd(memberRight);
                return Ok(res);//统一格式  数据data 状态码 code 提示信息message
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 获取权益列表
        /// </summary>
        /// <param name="totalCount">总条数</param>
        /// <param name="totalPage">总页数</param>
        /// <param name="rightName">权益名称查询</param>
        /// <param name="createTime">创建时间查询</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult MemberRightsList(string? rightName = "", DateTime? date1 = null, DateTime? date2 = null, int pageIndex = 1, int pageSize = 2)
        {
            try
            {
                //1.查询所有显示的数据
                int totalCount = 0;
                int totalPage = 0;
                List<MemberRight> list = MemberBLL.MemberRightsList(out totalCount, out totalPage, rightName, date1, date2, pageIndex, pageSize);

                return Ok(new
                {
                    list = list,
                    totalCount = totalCount,
                    totalPage = totalPage
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 会员权益逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpDelete]
        public IActionResult DelMemberRight(int id)
        {
            try
            {
                var res = MemberBLL.DelMemberRight(id);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 会员权益反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public IActionResult GetMemberRight(int id)
        {
            try
            {
                var res = MemberBLL.GetMemberRight(id);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 会员权益修改
        /// </summary>
        /// <param name="memberRight"></param>
        /// <returns></returns>
        /// 
        [HttpPut]
        public IActionResult GetMemberRightUpd(MemberRight memberRight)
        {
            try
            {
                var info = MemberBLL.GetMemberRightUpd(memberRight);
                return Ok(info);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据会员权益id修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        /// 
        [HttpPut]
        public IActionResult UpdateMemberRightState(int id, int state)
        {
            try
            {
                //1.根据id找到对应的实体
                var info = MemberBLL.UpdateMemberRightState(id, state);
                return Ok(info);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
